package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
@Validated
public class CategoryController {

    private  final CategoryService categoryService;


    @PostMapping
    public Result<String> addCategory(@RequestBody @Validated(Category.add.class) Category category){
        categoryService.addCategory(category);
        return Result.success();

    }
    @GetMapping
    public Result<List<Category>> queryCategoryList(){
        List<Category> categoryList = categoryService.queryCategoryList();
        return Result.success(categoryList);
    }
    @GetMapping("/detail")
    public Result<Category> categoryDetail(@RequestParam Integer id){
        Category category = categoryService.categoryDetail(id);
        return Result.success(category);

    }
    @PutMapping
    public Result<String> updateCategory(@RequestBody @Validated(Category.update.class) Category category){
        categoryService.updateCategory(category);
        return Result.success();

    }
    @DeleteMapping
    public Result<String> deleteCategoryById(@RequestParam Integer id){
        categoryService.deleteCategory(id);
        return Result.success();

    }

}
